package com.computercourse.ucenter.service;

import com.computercourse.framework.domain.ucenter.CicCompanyUser;
import com.computercourse.framework.domain.ucenter.CicMenu;
import com.computercourse.framework.domain.ucenter.CicUser;
import com.computercourse.framework.domain.ucenter.ext.CicUserExt;
import com.computercourse.framework.domain.ucenter.response.ModifyUserResult;
import com.computercourse.framework.model.response.CommonCode;
import com.computercourse.ucenter.dao.CicCompanyUserRepository;
import com.computercourse.ucenter.dao.CicMenuMapper;
import com.computercourse.ucenter.dao.CicUserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Optional;


@Service
public class UserService {

    @Autowired
    CicUserRepository cicUserRepository;

    @Autowired
    CicCompanyUserRepository cicCompanyUserRepository;

    @Autowired
    CicMenuMapper cicMenuMapper;


    //根据账号查询xcUser信息
    public CicUser findXcUserByUsername(String username) {
        return cicUserRepository.findByUsername(username);
    }

    //根据账号查询用户信息
    public CicUserExt getUserExt(String username) {
        //根据账号查询xcUser信息
        CicUser cicUser = this.findXcUserByUsername(username);
        if (cicUser == null) {
            return null;
        }
        //用户id
        String userId = cicUser.getId();
        //查询用户所有权限
        List<CicMenu> cicMenus = cicMenuMapper.selectPermissionByUserId(userId);

        //根据用户id查询用户所属公司id
        CicCompanyUser cicCompanyUser = cicCompanyUserRepository.findByUserId(userId);
        //取到用户的公司id
        String companyId = null;
        if (cicCompanyUser != null) {
            companyId = cicCompanyUser.getCompanyId();
        }
        CicUserExt cicUserExt = new CicUserExt();
        BeanUtils.copyProperties(cicUser, cicUserExt);
        cicUserExt.setCompanyId(companyId);
        //设置权限
        cicUserExt.setPermissions(cicMenus);
        return cicUserExt;

    }

    @Transactional
    public ModifyUserResult modifyUserName(String userName, String id) {

        Optional<CicUser> cicUser = cicUserRepository.findById(id);
        CicUser user = cicUser.get();
        CicUser user2 = new CicUser();
        if (cicUser.isPresent()) {
            user.setName(userName);
            cicUserRepository.save(user);
        } else {

            user2.setName(userName);
            user2.setId(id);
            cicUserRepository.save(user);
        }
        return new ModifyUserResult(CommonCode.SUCCESS, user);

    }

    @Transactional
    public ModifyUserResult modifyUserPhone(String userPhone, String id) {

        Optional<CicUser> cicUser = cicUserRepository.findById(id);
        CicUser user = cicUser.get();
        CicUser user2 = new CicUser();
        if (cicUser.isPresent()) {
            user.setPhone(userPhone);
            cicUserRepository.save(user);
        } else {

            user2.setPhone(userPhone);
            user2.setId(id);
            cicUserRepository.save(user);
        }
        return new ModifyUserResult(CommonCode.SUCCESS, user);

    }

    @Transactional
    public ModifyUserResult modifyUserPassword(String userPassword, String id) {

        Optional<CicUser> cicUser = cicUserRepository.findById(id);
        CicUser user = cicUser.get();
        CicUser user2 = new CicUser();
        if (cicUser.isPresent()) {
            user.setPassword(userPassword);
            cicUserRepository.save(user);
        } else {

            user2.setPassword(userPassword);
            user2.setId(id);
            cicUserRepository.save(user);
        }
        return new ModifyUserResult(CommonCode.SUCCESS, user);

    }

    @Transactional
    public ModifyUserResult modifyUserEmail(String userEmail, String id) {

        Optional<CicUser> cicUser = cicUserRepository.findById(id);
        CicUser user = cicUser.get();
        CicUser user2 = new CicUser();
        if (cicUser.isPresent()) {
            user.setEmail(userEmail);
            cicUserRepository.save(user);
        } else {

            user2.setEmail(userEmail);
            user2.setId(id);
            cicUserRepository.save(user);
        }
        return new ModifyUserResult(CommonCode.SUCCESS, user);

    }

    @Transactional
    public ModifyUserResult modifyUserQQ(String userQQ, String id) {

        Optional<CicUser> cicUser = cicUserRepository.findById(id);
        CicUser user = cicUser.get();
        CicUser user2 = new CicUser();
        if (cicUser.isPresent()) {
            user.setQq(userQQ);
            cicUserRepository.save(user);
        } else {

            user2.setQq(userQQ);
            user2.setId(id);
            cicUserRepository.save(user);
        }
        return new ModifyUserResult(CommonCode.SUCCESS, user);

    }

    @Transactional
    public ModifyUserResult uploadUserImage(MultipartFile multipartFile, String id) throws Exception {

        Optional<CicUser> cicUser = cicUserRepository.findById(id);
        CicUser cicUser1 = cicUser.get();
        if (cicUser.isPresent()) {
            String storePath = "/Users/yuanfeng/Documents/bysj-computer-introduction-course-ui-pc-learning/static/images/";
            //上传文件
            //得到文件字节
            byte[] bytes = multipartFile.getBytes();
            //得到文件的原始名称
            String originalFilename = multipartFile.getOriginalFilename();
            //得到文件扩展名
            String fileExtName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            File file = new File(storePath + originalFilename);

            FileOutputStream outputStream = new FileOutputStream(file);
            String currentFilePath = storePath + originalFilename;
            outputStream.write(bytes);
            outputStream.close();

            cicUser1.setUserpic("/static/images/" + originalFilename);
            cicUserRepository.save(cicUser1);
        }
        return new ModifyUserResult(CommonCode.SUCCESS, cicUser1);

    }


}
